package client.gui.lobby;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class LoginPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField = null;
	private JLabel jLabel2 = null;
	private JPasswordField jPasswordField = null;
	private JButton jButton1 = null;
	// private ClientGUI frame = null;
	private JTextField hostTextField = null;
	private JLabel hostLabel = null;
	JFrame parent = null;

	// private GridBagConstraints chatLocation = null;

	public LoginPanel(JFrame loginFrame) {
		super();
		parent = loginFrame;
		initialize();
	}

	public LoginPanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
		gridBagConstraints7.fill = GridBagConstraints.BOTH;
		gridBagConstraints7.gridy = 1;
		gridBagConstraints7.weightx = 1.0;
		gridBagConstraints7.anchor = GridBagConstraints.WEST;
		gridBagConstraints7.gridx = 3;
		GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
		gridBagConstraints6.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints6.gridy = 2;
		gridBagConstraints6.weightx = 1.0;
		gridBagConstraints6.gridwidth = 1;
		gridBagConstraints6.anchor = GridBagConstraints.WEST;
		gridBagConstraints6.gridx = 3;
		GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
		gridBagConstraints5.gridx = 0;
		gridBagConstraints5.gridy = 4;
		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
		gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints4.gridy = 4;
		gridBagConstraints4.weightx = 1.0;
		gridBagConstraints4.anchor = GridBagConstraints.WEST;
		gridBagConstraints4.gridx = 3;
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.gridx = 6;
		gridBagConstraints3.gridy = 6;
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.gridy = 2;
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.gridx = 0;
		gridBagConstraints1.gridy = 1;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		this.setLayout(new GridBagLayout());
		hostLabel = new JLabel();
		hostLabel.setText("Host:");
		jLabel2 = new JLabel();
		jLabel2.setText("Password:");
		jLabel1 = new JLabel();
		jLabel1.setText("User Name:");
		jLabel = new JLabel();
		jLabel.setText("Login:");
		this.setSize(437, 152);
		this.add(jLabel, gridBagConstraints);
		this.add(jLabel1, gridBagConstraints1);
		this.add(getJTextField(), gridBagConstraints7);
		this.add(jLabel2, gridBagConstraints2);
		this.add(getJPasswordField(), gridBagConstraints6);
		this.add(getButton1(), gridBagConstraints3);
		this.add(getHostTextField(), gridBagConstraints4);
		this.add(hostLabel, gridBagConstraints5);
	}

	/**
	 * This method initializes jTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setPreferredSize(new Dimension(300, 24));
			jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyTyped(java.awt.event.KeyEvent e) {
					if (e.getKeyChar() == '\n')
						login();
				}
			});
			jTextField.requestFocus();
		}
		return jTextField;
	}

	/**
	 * This method initializes jPasswordField
	 * 
	 * @return javax.swing.JPasswordField
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
			jPasswordField.setPreferredSize(new Dimension(300, 20));
			jPasswordField.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyTyped(java.awt.event.KeyEvent e) {
					if (e.getKeyChar() == '\n')
						login();
				}
			});
		}
		return jPasswordField;
	}

	/**
	 * This method initializes jToggleButton1
	 * 
	 * @return javax.swing.JToggleButton
	 */
	private JButton getButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Login:");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					login();
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes hostTextField
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getHostTextField() {
		if (hostTextField == null) {
			hostTextField = new JTextField();
			hostTextField.setBounds(new Rectangle(105, 135, 178, 18));
			hostTextField.setText("localhost:11011");
		}
		return hostTextField;
	}

	private void login() {
		String login;
		login = jTextField.getText();
		String hostField = hostTextField.getText();
		String host = "localhost";
		int port = 11011;
		int a = hostField.indexOf(":");
		if (a == -1)
			host = hostField;
		else {
			host = hostField.substring(0, a);
			port = Integer.parseInt(hostField.substring(a + 1));
		}
		String password = "";
		if (jPasswordField.getPassword() != null)
			for (char c : jPasswordField.getPassword()) {
				password += c;
			}
		Lobby lobby = new Lobby();
		lobby.setConnectionDetails(host, port, login, password);
		if (lobby.connect()) {
			if (parent != null) {
				parent.setVisible(false);
				parent.dispose();
			}
			lobby.setVisible(true);
		}
		/*
		 * frame.listener = cp.listener; frame.add(cp, gridBagConstraints);
		 * frame.listener.gui=frame; frame.remove(this);
		 */
	}

} // @jve:decl-index=0:visual-constraint="0,6"
